using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Collections.Specialized;
using System.Text;

namespace AresControls
{
	public class HDate : HControl, IPostBackDataHandler
	{
		public const string PATTERN_DD_MM_YYYY = "dd/MM/yyyy";
		public const string PATTERN_MM_DD_YYYY = "MM/dd/yyyy";
		public const string PATTERN_MM_YYYY = "MM/yyyy";
		public const string PATTERN_YYYY = "yyyy";
		public const string PATTERN_DD_MM_YYYY_hh_mm_ss = "dd/MM/yyyy HH:mm:ss";
		public const string PATTERN_MM_DD_YYYY_hh_mm_ss = "MM/dd/yyyy HH:mm:ss";

		private string m_strBorder, m_strPathPrefix, m_strLanguage, m_strColor, m_strBackground, m_strSelectedBackground, m_strReadOnlyBackground;
		private bool m_PartialReadOnly = false;

		public HDate()
		{
			this.Pattern = HDate.PATTERN_DD_MM_YYYY;
			this.Value = GetDateTimeFormated(System.DateTime.Now, this.Pattern, this.Pattern);

			this.InvokeOnChange = "";
			this.Color = "#442200";
			this.Background = "white";
			this.SelectedBackground = "white";
			this.ReadOnlyBackground = "#dedede";
			this.Width = "72";
			this.Style = "";
			m_strBorder = "2";
		}

		public string Value
		{
			get { return (ViewState["Value"] == null ? "" : (string)ViewState["Value"]); }
			set { ViewState["Value"] = (value == "--/--/----" ? "" : value); }
		}

		public string Pattern
		{
			get { return ViewState["Pattern"].ToString(); }
			set
			{
				DateTime dt = System.DateTime.Now;
				if (this.Value != "" && this.Value != "----" && this.Value != "--/----" && this.Value != "--/--/----" && this.Value != "--/--/---- --:--:--")
					dt = this.DateTimeValue;

				ViewState["Pattern"] = (value == HDate.PATTERN_DD_MM_YYYY || value == HDate.PATTERN_MM_DD_YYYY || value == HDate.PATTERN_MM_YYYY || value == HDate.PATTERN_YYYY || value == HDate.PATTERN_DD_MM_YYYY_hh_mm_ss || value == HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? value : HDate.PATTERN_DD_MM_YYYY);

				if (this.Value != "" && this.Value != "----" && this.Value != "--/----" && this.Value != "--/--/----" && this.Value != "--/--/---- --:--:--")
					this.Value = dt.ToString(ViewState["Pattern"].ToString());

				if (this.Width != "0")
				{
					this.Width = "72";
					if (this.Pattern == HDate.PATTERN_YYYY)
						this.Width = "40";
					if (this.Pattern == HDate.PATTERN_MM_YYYY)
						this.Width = "56";
					if (this.Pattern == HDate.PATTERN_DD_MM_YYYY_hh_mm_ss || this.Pattern == HDate.PATTERN_MM_DD_YYYY_hh_mm_ss)
						this.Width = "120";
				}
			}
		}

		public string Border
		{
			get { return m_strBorder; }
			set { m_strBorder = value; }
		}

		public string Color
		{
			get { return m_strColor; }
			set { m_strColor = value; }
		}

		public string Background
		{
			get { return m_strBackground; }
			set { m_strBackground = value; }
		}

		public string ReadOnlyBackground
		{
			get { return m_strReadOnlyBackground; }
			set { m_strReadOnlyBackground = value; }
		}

		public string SelectedBackground
		{
			get { return m_strSelectedBackground; }
			set { m_strSelectedBackground = value; }
		}

		public bool PartialReadOnly
		{
			get { return m_PartialReadOnly; }
			set { m_PartialReadOnly = value; }
		}

		public string Language
		{
			get { return m_strLanguage; }
			set { m_strLanguage = value; }
		}

		public string PathPrefix
		{
			get { return m_strPathPrefix; }
			set { m_strPathPrefix = value; }
		}

		public DateTime DateTimeValue
		{
			get
			{
				string strPatternOld = (this.Pattern != HDate.PATTERN_MM_YYYY && this.Pattern != HDate.PATTERN_YYYY ? this.Pattern : HDate.PATTERN_DD_MM_YYYY);

				string s = this.Value;
				if (this.Pattern == HDate.PATTERN_YYYY)
				{
					if (s.Length < 4)
						throw new FormatException();
					s = "01/01/" + s.Substring(s.Length - 4);
				}
				if (this.Pattern == HDate.PATTERN_MM_YYYY)
				{
					if (s.Length < 7)
						throw new FormatException();
					s = "01/" + s.Substring(s.Length - 7);
				}

				CultureInfo ci = new CultureInfo("en-US");
				DateTimeFormatInfo dtfi = ci.DateTimeFormat;
				dtfi.ShortDatePattern = strPatternOld;
				return DateTime.Parse(s, dtfi);
			}
		}

		public string FormattedValue(string strPattern)
		{
			if (this.Value == "")
				return "";
			else
				return GetDateTimeFormated(DateTimeValue, strPattern, "");
		}

		public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
		{
			if (!this.GridControl)
				if (this.Value != postCollection[postDataKey])
				{
					this.Value = postCollection[postDataKey];
					return true;
				}
			return false;
		}

		public void RaisePostDataChangedEvent()
		{
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			this.PathPrefix = this.Page.Request.ApplicationPath + "/";
			VerifyValue();
			if (this.Value != "")
				TrySetValue();
		}

		private string GetDateTimeFormated(DateTime d, string strPattern, string strShortDatePattern)
		{
			CultureInfo ci = new CultureInfo("en-US");
			DateTimeFormatInfo dtfi = ci.DateTimeFormat;
			if (strShortDatePattern != "")
				dtfi.ShortDatePattern = strShortDatePattern;
			return d.ToString(strPattern, dtfi);
		}

		private string GetValue()
		{
			if (this.Value == "")
				switch (this.Pattern)
				{
					case HDate.PATTERN_DD_MM_YYYY_hh_mm_ss:
					case HDate.PATTERN_MM_DD_YYYY_hh_mm_ss:
						return "--/--/---- --:--:--";
					case HDate.PATTERN_DD_MM_YYYY:
					case HDate.PATTERN_MM_DD_YYYY:
						return "--/--/----";
					case HDate.PATTERN_MM_YYYY:
						return "--/----";
					case HDate.PATTERN_YYYY:
						return "----";
					default:
						return "--/--/----";
				}
			else
				return this.Value;
		}

		protected void TrySetValue()
		{
			try
			{
				this.Value = FormattedValue(this.Pattern);
			}
			catch(FormatException)
			{
				this.Value = "";
			}
		}

		private void VerifyValue()
		{
			if (this.Value == "----" || this.Value == "--/----" || this.Value == "--/--/----" || this.Value == "--/--/---- --:--:--")
				this.Value = "";
			if (this.Value.Length > 10 && this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss)
				this.Value = GetDateTimeFormated(DateTime.Parse(this.Value), this.Pattern, this.Pattern);
		}

		protected override void Render(HtmlTextWriter output)
		{
			string strUniqueID = this.UniqueID;
			StringBuilder sb = new StringBuilder();

			if (this.GridControl)
				this.Border = "0";

			int nSpan0Width = 0;
			if (this.Width != "0")
				switch (this.Pattern)
				{
					case HDate.PATTERN_MM_YYYY:
						nSpan0Width = 58;
						break;
					case HDate.PATTERN_YYYY:
						nSpan0Width = 42;
						break;
					case HDate.PATTERN_DD_MM_YYYY_hh_mm_ss:
					case HDate.PATTERN_MM_DD_YYYY_hh_mm_ss:
						if (this.GridControl)
							nSpan0Width = 120;
						else
							nSpan0Width = 124;
						break;
					default:
						nSpan0Width = 72 + 2 * Int32.Parse(this.Border);
						break;
				}

			VerifyValue();

			sb.Append("<span onactivate=\"if(this.childNodes(0).style.display == \'\')this.childNodes(0).setActive();\" ControlType=\"HDate\" style=\"border: ").Append(this.Border).Append("px; border-style: inset; width: ").Append(nSpan0Width).Append("px; ").Append("padding-top: 1px; padding-left: 1px; background: ").Append((this.ReadOnly ? this.ReadOnlyBackground : this.Background)).Append("; ").Append(this.Style).Append("\"");
			sb.Append(this.DefaultValue != null ? " DefaultValue=\"" + this.DefaultValue + "\"" : "");
			sb.Append(this.Required ? renderRequired() : "");
			sb.Append(this.ReadOnly ? renderControlReadOnly() : "");
			sb.Append(this.GridControl ? renderGridControl() : "");
			sb.Append(" AlertRequiredString=\"").Append(this.AlertRequiredString).Append("\"");
			sb.Append(" GridId=\"").Append(this.GridId).Append("\"");
			sb.Append(" Background=\"").Append((this.ReadOnly ? this.ReadOnlyBackground : this.Background)).Append("\"");
			sb.Append(" SelectedBackground=\"").Append(this.SelectedBackground).Append("\">");
				sb.Append("<input value=\"").Append(GetValue()).Append("\" type=\"text\" class=\"HDateNewText\" ID=\"").Append(strUniqueID).Append("\" name=\"").Append(strUniqueID).Append("\" style=\"width: 100%; background: transparent;");
				sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
				sb.Append("\" OnActivate=\"OnActivate_HDate();\" Pattern=\"").Append(this.Pattern).Append("\" HDateLanguage=\"").Append(this.Language).Append("\" InvokeOnChange=\"").Append(this.InvokeOnChange).Append("\"");
				sb.Append(this.ReadOnly ? " readonly tabindex=-1" : "");
				sb.Append(" ondragenter=\"event.returnValue=false;\" ");
				sb.Append(" GridId=\"").Append(this.GridId).Append("\"/>");

				sb.Append("<span style=\"display: none; margin-left: 1px;\" OnBeforeDeactivate=\"OnBeforeDeactivate_HDate();\" OnDeactivate=\"OnDeactivate_HDate();\">");
					sb.Append("<input type=\"text\" class=\"HDateNewText\" style=\"margin-top: 1px; width: 13px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern == HDate.PATTERN_YYYY || this.Pattern == HDate.PATTERN_MM_YYYY ? " display: none;" : "")).Append("\"");
					sb.Append(" ondragenter=\"event.returnValue=false;\" MaxLength=\"2\" ");
					sb.Append(" OnActivate=\"OnActivate_Input(1);\" OnBeforeDeactivate=\"OnBeforeDeactivate_Input();\" OnClick=\"OnClick_Input(1);\" OnKeyPress=\"OnKeyPress_Input();\" OnKeyDown=\"OnKeyDown_Input();\" OnKeyUp=\"OnKeyUp_Input(true);\" OnMouseWheel=\"OnMouseWheel_Input();\"/>");

					sb.Append("<span class=\"HDateNewText\" style=\"margin-top: 1px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern == HDate.PATTERN_YYYY || this.Pattern == HDate.PATTERN_MM_YYYY ? "display: none;" : "")).Append("\">/</span>");

					sb.Append("<input type=\"text\" class=\"HDateNewText\" style=\"margin-top: 1px; width: 13px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern == HDate.PATTERN_YYYY ? " display: none;" : "")).Append("\"");
					sb.Append(" ondragenter=\"event.returnValue=false;\" MaxLength=\"2\" ");
					sb.Append(" OnActivate=\"OnActivate_Input(2);\" OnBeforeDeactivate=\"OnBeforeDeactivate_Input();\" OnClick=\"OnClick_Input(2);\" OnKeyPress=\"OnKeyPress_Input();\" OnKeyDown=\"OnKeyDown_Input();\" OnKeyUp=\"OnKeyUp_Input(true);\" OnMouseWheel=\"OnMouseWheel_Input();\"/>");

					sb.Append("<span class=\"HDateNewText\" style=\"margin-top: 1px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern == HDate.PATTERN_YYYY ? "display: none;" : "")).Append("\">/</span>");

					sb.Append("<input type=\"text\" class=\"HDateNewText\" style=\"margin-top: 1px; width: 26px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append("\"");
					sb.Append(" ondragenter=\"event.returnValue=false;\" MaxLength=\"4\" ");
					sb.Append(" OnActivate=\"OnActivate_Input(3);\" OnBeforeDeactivate=\"OnBeforeDeactivate_Input();\" OnClick=\"OnClick_Input(3);\" OnKeyPress=\"OnKeyPress_Input();\" OnKeyDown=\"OnKeyDown_Input();\" OnKeyUp=\"OnKeyUp_Input(true);\" OnMouseWheel=\"OnMouseWheel_Input();\"/>");

					sb.Append("<span class=\"HDateNewText\" style=\"margin-top: 1px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? " display: none;" : "")).Append("\"");
					sb.Append("\">&nbsp;</span>");

					sb.Append("<input type=\"text\" class=\"HDateNewText\" style=\"margin-top: 1px; width: 13px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? " display: none;" : "")).Append("\"");
					sb.Append(" ondragenter=\"event.returnValue=false;\" MaxLength=\"2\" ");
					sb.Append(" OnActivate=\"OnActivate_Input(4);\" OnBeforeDeactivate=\"OnBeforeDeactivate_Input();\" OnClick=\"OnClick_Input(4);\" OnKeyPress=\"OnKeyPress_Input();\" OnKeyDown=\"OnKeyDown_Input();\" OnKeyUp=\"OnKeyUp_Input(true);\" OnMouseWheel=\"OnMouseWheel_Input();\"/>");

					sb.Append("<span class=\"HDateNewText\" style=\"margin-top: 1px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? " display: none;" : "")).Append("\"");
					sb.Append("\">:</span>");

					sb.Append("<input type=\"text\" class=\"HDateNewText\" style=\"margin-top: 1px; width: 13px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? " display: none;" : "")).Append("\"");
					sb.Append(" ondragenter=\"event.returnValue=false;\" MaxLength=\"2\" ");
					sb.Append(" OnActivate=\"OnActivate_Input(5);\" OnBeforeDeactivate=\"OnBeforeDeactivate_Input();\" OnClick=\"OnClick_Input(5);\" OnKeyPress=\"OnKeyPress_Input();\" OnKeyDown=\"OnKeyDown_Input();\" OnKeyUp=\"OnKeyUp_Input(true);\" OnMouseWheel=\"OnMouseWheel_Input();\"/>");

					sb.Append("<span class=\"HDateNewText\" style=\"margin-top: 1px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? " display: none;" : "")).Append("\"");
					sb.Append("\">:</span>");

					sb.Append("<input type=\"text\" class=\"HDateNewText\" style=\"margin-top: 1px; width: 13px;");
					sb.Append(this.GridControl ? " color: " + this.Color + ";" : "");
					sb.Append((this.Pattern != HDate.PATTERN_DD_MM_YYYY_hh_mm_ss && this.Pattern != HDate.PATTERN_MM_DD_YYYY_hh_mm_ss ? " display: none;" : "")).Append("\"");
					sb.Append(" ondragenter=\"event.returnValue=false;\" MaxLength=\"2\" ");
					sb.Append(" OnActivate=\"OnActivate_Input(6);\" OnBeforeDeactivate=\"OnBeforeDeactivate_Input();\" OnClick=\"OnClick_Input(6);\" OnKeyPress=\"OnKeyPress_Input();\" OnKeyDown=\"OnKeyDown_Input();\" OnKeyUp=\"OnKeyUp_Input(true);\" OnMouseWheel=\"OnMouseWheel_Input();\"/>");

					sb.Append("<span style=\"border: none; height: 17px; vertical-align: middle; writing-mode: tb-rl;\">");
					sb.Append("<input type=\"button\" class=\"HDateNewButtonUpDown\" style=\"background-image: url(").Append(this.PathPrefix).Append("Resources/Images/Tools/UpArrowButton.jpg); \" tabindex=\"-1\" OnClick=\"OnClick_UpDown(1);\"/>");
					sb.Append("<input type=\"button\" class=\"HDateNewButtonUpDown\" style=\"background-image: url(").Append(this.PathPrefix).Append("Resources/Images/Tools/DownArrowButton.jpg); \" tabindex=\"-1\" OnClick=\"OnClick_UpDown(-1);\"/>");
					sb.Append("</span>");
				sb.Append("</span>");
			sb.Append("</span>");

			output.Write(sb.ToString());
		}

	}
}
